package com.core.user.bo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.core.role.bo.RoleBo;
import com.core.role.pojo.Role;
import com.core.user.SuperUserName;
import com.core.user.dao.UserDao;
import com.core.user.dto.UserDto;
import com.core.user.dto.UserSearchCondition;
import com.core.user.pojo.User;
import com.util.StringUtil;
import com.util.exception.ComRuntimeException;
@Service
public class UserBo {
	@Autowired
    private UserDao userDao;

    @Autowired
    private RoleBo roleBo;

    public User getById(User user) {
        return userDao.getById(user);
    }

    public void add(User user) {
        validateAddUpdate(user, true);
        userDao.add(user);
    }

    public void update(User user) {
        validateAddUpdate(user, false);
        userDao.update(user);
    }

    private void validateAddUpdate(User user, boolean isAdd) {
        if (user == null) {
            throw new ComRuntimeException("用户实体不能为空");
        }

        if (StringUtil.isEmpty(user.getLoginName())) {
            throw new ComRuntimeException("登录名不能为空");
        } else if (user.getLoginName().length() > 20) {
            throw new ComRuntimeException("用户名不能大于20位");
        }


        if (StringUtil.isEmpty(user.getPassword())) {
            throw new ComRuntimeException("密码不能为空");
        } else if (user.getPassword().length() > 20) {
            throw new ComRuntimeException("密码不能大于20位");
        }

        //////////////////
        User user1 = getByUserName(user.getLoginName());
        if (isAdd) {
            if (user1 != null) {
                throw new ComRuntimeException("登录名已存在，请重新输入");
            }
        } else {
            if (user.getId() == null) {
                throw new ComRuntimeException("用户id不能为空");
            }

            if (user1 != null && user1.getId() != user.getId().intValue()) {
                throw new ComRuntimeException("用户名已存在，请重新输入");
            }
        }
    }

    public void delete(User user) {
        User user1 = getById(user);
        if (user1 == null) {
            return;
        }

        if (SuperUserName.ADMIN.equals(user1.getLoginName())) {
            throw new ComRuntimeException("不能删除超级管理员");
        }

        userDao.delete(user);
    }

    public List<User> searchPojo(UserSearchCondition sc) {
        return userDao.searchPojo(sc);
    }

    public List<UserDto> searchDto(UserSearchCondition sc) {
        return userDao.searchDto(sc);
    }

    public Integer searchCnt(UserSearchCondition sc) {
        return userDao.searchCnt(sc);
    }

    //======================================= new method create here ====================================/
    public User getByUserName(String userName) {
        return userDao.getByUserName(userName);
    }

    public User login(String userName, String password) {
        User user = getByUserName(userName);

        if (user == null) {
            throw new ComRuntimeException("用户名不存在");
        }

        if (!user.getPassword().equals(password)) {
            throw new ComRuntimeException("密码错误");
        }

//        if (!UserStateEnum.enabled(user.getStateId())) {
//            throw new ComRuntimeException("您的帐户已被禁用");
//        }

        List<Role> roleList = roleBo.searchPojoByUserId(user);
        user.setRoleList(roleList);

        return user;
    }


    public void updatePwd(String userName, String oldPwd, String newPwd) {
        User user = getByUserName(userName);
        if (user == null) {
            throw new ComRuntimeException("用户名不存在");
        }

        if (!user.getPassword().equals(oldPwd)) {
            throw new ComRuntimeException("原密码错误");
        }

        if (StringUtil.isEmpty(newPwd)) {
            throw new ComRuntimeException("新密码不能为空");
        } else if (newPwd.length() > 20) {
            throw new ComRuntimeException("新密码不能大于20位");
        }

        //direct update
        user.setPassword(newPwd);
        userDao.update(user);
    }

}
